package be.vdab.dao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import be.vdab.entities.Persoon;

@Repository("persoonDAOCSV")
@Qualifier("persoonCSV")
public class PersoonDAOCSV implements PersoonDAO {
	private File bestand;
	
	@Autowired
	public PersoonDAOCSV(@Value("${persoonCSV}") File bestand){
		this.bestand = bestand;
		System.out.println("constructor " + this.getClass().getName());
	}
	@Override
	public Iterable<Persoon> findAll(){
		List<Persoon> personen = new ArrayList<Persoon>();
		try(BufferedReader reader = new BufferedReader(new FileReader(bestand))){
			for(String regel; (regel = reader.readLine()) != null;){
				String[] onderdelen = regel.split(",");
				personen.add(new Persoon(Integer.parseInt(onderdelen[0]), onderdelen[1], onderdelen[2], Integer.parseInt(onderdelen[3])));
			}
		}catch(IOException ex){
			throw new PersoonDAOException(ex);
		}
		return personen;
	}
}
